import os
import pprint
hosts = os.listdir('urlhome')
template = '''
            <VirtualHost 210.72.13.40>
                ServerName {host}
                ServerAlias {alias}
                DocumentRoot /www/urlhome/{host}/
                ErrorLog /www/apache/logs/error_log
            </VirtualHost>
            '''
alias_dict = {}
for host in hosts:
    if host == 'urlhome':
        continue
    if host.startswith('www.'):
        alias_dict.update({host: host.replace('www.', '')})
    else:
        alias_dict.update({host: 'www.' + host})

with open('vhosts.conf', 'wb') as vhost:
    for host in sorted(alias_dict, key=lambda x:x):
        vhost.write(template.format(host=host, alias=alias_dict[host]))
